package com.manager.commons.encrypt.des;

//                   _ooOoo_    
//                  o8888888o    
//                  88" . "88    
//                  (| -_- |)    
//                   O\ = /O    
//               ____/'---'\____    
//              /. '\\| : |//' .\    
//             // \\||| : |||// \\   
//            // _|||||-:-|||||_ \\    
//           // | | \\\ - /// | | \\
//           || |_| ''\---/'' |_| || 
//            \ .-\___/'-'\___/-. /    
//          ___\. .' /--.--\ '. ./___    
//        .""'< `.___\_<|>_/___.' >'"".    
//       | |: `- \`.|'\ _ /'|.`/ -` :| |    
//        \ \ `-. \_ __\ /__ _/ .-` / /    
//======'-.___`-.___\_______/___.-`___.-'======    
//                   '=---='    
//.................................................    
//写一哈代码，你就青叫唤；信龟儿设计，你逗是傻逼。
//永无Bug，啷个可能！求佛主保佑也没球用！

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

/**
 * Des
 * @author H
 * @version V1.0
 */
public class Des {

	private static final String KEY_ALGORITHM = "DESede";
	private static final String DEFAULT_CIPHER_ALGORITHM = "DESede/CBC/PKCS5Padding";
	private static final String DEFAULT_CHARSET = "utf-8";
	private static final String DEFAULT_IVP = "01234567";

	/**
	 * 加密
	 * @param text
	 * @param key
	 * @return
	 */
	public static String encrypt(String text, String key) {
		return encrypt(text, key, DEFAULT_IVP);
	}

	/**
	 * 加密
	 * @param content
	 * @param key
	 * @param ivp
	 * @return
	 */
	public static String encrypt(String text, String key, String ivp) {
		return encrypt(text, key, ivp, DEFAULT_CHARSET);
	}

	/**
	 * 加密
	 * @param text
	 * @param key
	 * @param ivp
	 * @param charset
	 * @return
	 */
	public static String encrypt(String text, String key, String ivp, String charset) {
		try {
			Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
			cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(key), new IvParameterSpec(ivp.getBytes()));
			byte[] result = cipher.doFinal(text.getBytes(charset));
			return Base64.encodeBase64String(result);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}

	/**
	 * 解密
	 * @param content
	 * @param key
	 * @param ivp
	 * @return
	 */
	public static String decrypt(String text, String key) {
		return decrypt(text, key, DEFAULT_IVP);
	}

	/**
	 * 解密
	 * @param content
	 * @param key
	 * @param ivp
	 * @return
	 */
	public static String decrypt(String text, String key, String ivp) {
		return decrypt(text, key, ivp, DEFAULT_CHARSET);
	}

	/**
	 * 解密
	 * @param content
	 * @param key
	 * @param ivp
	 * @return
	 */
	public static String decrypt(String text, String key, String ivp, String charset) {
		try {
			Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
			cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key), new IvParameterSpec(ivp.getBytes()));
			byte[] result = cipher.doFinal(Base64.decodeBase64(text));
			return new String(result, charset);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}

	/**
	 * 生成加密秘钥
	 * @return
	 */
	private static SecretKeySpec getSecretKey(final String key) {
		KeyGenerator kg = null;
		try {
			kg = KeyGenerator.getInstance(KEY_ALGORITHM);
			kg.init(new SecureRandom(key.getBytes()));
			SecretKey secretKey = kg.generateKey();
			return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return null;
	}
}